//
//  AroundShopCell.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/12.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "AroundShopCell.h"
#import "ShopEntity.h"
#import "ANGradientButton.h"

@interface AroundShopCell ()

@property (nonatomic, strong) UIButton *typeButton;
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *shopNameLab;
@property (nonatomic, strong) UIImageView *locIcon;
@property (nonatomic, strong) UILabel *locLab;
@property (nonatomic, strong) UIImageView *telIcon;
@property (nonatomic, strong) UILabel *telLab;
@property (nonatomic, strong) UILabel *juliLab;

@property (nonatomic, strong) ANGradientButton *appointmentBtn;

@end

@implementation AroundShopCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubViews];
    }
    return self;
}

- (void)appointmentAction:(id)sender
{
    if (self.appointmentHandler) {
        self.appointmentHandler();
    }
}

- (void)setupSubViews
{
    self.icon = [[UIImageView alloc] init];
    self.icon.image = [UIImage imageNamed:@"pic_moren"];
    self.icon.layer.cornerRadius = 4;
    self.icon.layer.masksToBounds = YES;
    [self addSubview:self.icon];
 
    self.typeButton = [[UIButton alloc] init];
    self.typeButton.contentEdgeInsets = UIEdgeInsetsMake(3, 3, 3, 3);
    [self.typeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.typeButton.titleLabel.font = [UIFont systemFontOfSize:10];
    self.typeButton.userInteractionEnabled = NO;
    self.typeButton.layer.cornerRadius = 2;
    self.typeButton.layer.masksToBounds = YES;
    [self addSubview:self.typeButton];
    
    self.shopNameLab = [[UILabel alloc] init];
    self.shopNameLab.font = [UIFont boldSystemFontOfSize:14];
    self.shopNameLab.textColor = kColor333333;
    [self addSubview:self.shopNameLab];
    
    self.locIcon = [[UIImageView alloc] init];
    self.locIcon.image = [UIImage imageNamed:@"i_address"];
    [self addSubview:self.locIcon];
    
    self.locLab = [[UILabel alloc] init];
    self.locLab.numberOfLines = 2;
    self.locLab.textColor = kColor666666;
    self.locLab.font = [UIFont systemFontOfSize:13];
    [self addSubview:self.locLab];
    
    self.juliLab = [[UILabel alloc] init];
    self.juliLab.font = [UIFont systemFontOfSize:13];
    self.juliLab.textColor = kColor666666;
    [self addSubview:self.juliLab];
    
    self.telIcon = [[UIImageView alloc] init];
    self.telIcon.image = [UIImage imageNamed:@"i_tel"];
    [self addSubview:self.telIcon];
    
    self.telLab = [[UILabel alloc] init];
    self.telLab.textColor = kColor666666;
    self.telLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.telLab];
    
    self.appointmentBtn = [[ANGradientButton alloc] initWithFrame:CGRectMake(0, 0, kWidth(60), kWidth(24))];
    [self.appointmentBtn setTitle:@"预约" forState:UIControlStateNormal];
    [self.appointmentBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.appointmentBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.appointmentBtn];
    self.appointmentBtn.layer.cornerRadius = kWidth(12);
    self.appointmentBtn.layer.masksToBounds = YES;
    [self.appointmentBtn addTarget:self action:@selector(appointmentAction:) forControlEvents:UIControlEventTouchUpInside];
     
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(kWidth(16));
        make.top.equalTo(self).offset(kWidth(10));
        make.width.equalTo(@(kWidth(115)));
        make.height.equalTo(@(75/115.0*kWidth(115)));
    }];
    
    [self.typeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.icon);
        make.left.equalTo(self.icon.mas_right).offset(kWidth(10));
    }];
    
    [self.shopNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.icon);
        make.centerY.equalTo(self.typeButton);
        make.left.equalTo(self.typeButton.mas_right).offset(kWidth(8));
        make.right.lessThanOrEqualTo(self).offset(-kWidth(10));
    }];
    
    [self.locIcon setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.locIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.typeButton);
        make.top.equalTo(self.shopNameLab.mas_bottom).offset(kWidth(10));
//        make.width.height.equalTo(@20);
    }];
    
    [self.locLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.locLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.locIcon).offset(-kWidth(2));
        make.left.equalTo(self.locIcon.mas_right).offset(kWidth(5));
        make.right.lessThanOrEqualTo(self).offset(-kWidth(10));
    }];
    
    [self.juliLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.locLab.mas_bottom).offset(kWidth(10));
        make.left.equalTo(self.locIcon);
    }];
    
    [self.telIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.typeButton);
        make.top.equalTo(self.juliLab.mas_bottom).offset(kWidth(10));
        make.bottom.equalTo(self).offset(-kWidth(10));
//        make.bottom.equalTo(self.icon);
//        make.width.height.equalTo(@20);
    }];
    
    [self.telLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.telIcon);
        make.left.equalTo(self.telIcon.mas_right).offset(kWidth(5));
        make.right.lessThanOrEqualTo(self).offset(-kWidth(10));
    }];
    
    [self.appointmentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.locLab.mas_bottom).offset(kWidth(10));
        make.right.equalTo(self).offset(-kWidth(15));
    }];
    
//    self.shopNameLab.text = @"京东方is积分圣诞节";
//    self.locLab.text = @"机场东";
//    self.telLab.text = @"88888888";
}

- (void)setShopEntity:(ShopEntity *)shopEntity
{
    _shopEntity = shopEntity;
    self.shopNameLab.text = String(shopEntity.orgName);
    self.locLab.text = String(shopEntity.address);
    self.telLab.text = String(shopEntity.telephone);
    if (shopEntity.juli.length) {
        float juli = shopEntity.juli.floatValue;
        if (juli >= 1000) {
            self.juliLab.text = [NSString stringWithFormat:@"%.2fkm",juli/1000];
        }
        else {
            self.juliLab.text = [NSString stringWithFormat:@"%@m",shopEntity.juli];
        }
    }
    
    [self.icon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SERVERADDRS,shopEntity.filePath]] placeholderImage:PlaceHolderImage];
    switch (shopEntity.merchantType.intValue) {
        case 1:
            [self.typeButton setTitle:@"直营店" forState:UIControlStateNormal];
            self.typeButton.backgroundColor = kColorFF4444;
            break;
        case 2:
            [self.typeButton setTitle:@"供应商" forState:UIControlStateNormal];
            self.typeButton.backgroundColor = kColorFFB33B;
            break;
        case 3:
            [self.typeButton setTitle:@"合作商" forState:UIControlStateNormal];
            self.typeButton.backgroundColor = kColorA08FDF;
            break;
        case 4:
            [self.typeButton setTitle:@"加盟店" forState:UIControlStateNormal];
            self.typeButton.backgroundColor = kColor7CBB7D;
            break;
            
        default:
            [self.typeButton setTitle:@"" forState:UIControlStateNormal];
            self.typeButton.backgroundColor = [UIColor clearColor];
            break;
    }
}

@end
